package com.tingyuge.base.thread.concurrent2;

import java.util.Random;
import java.util.concurrent.*;

/**
 * @author xiaodao
 * @version 1.0
 * @date 2021/3/14
 */
public class Future1 {

    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        Callable<Integer> callable=()->{
            System.out.println("pre execution");
            Thread.sleep(5000);
            int i = new Random().nextInt(500);
            System.out.println("post execution");
            return i;
        };

        FutureTask<Integer> futureTask = new FutureTask<>(callable);

        new Thread(futureTask).start();
        System.out.println("thread start");
        Thread.sleep(2000);

        System.out.println(futureTask.get(1, TimeUnit.MICROSECONDS));
    }
}
